import QtQuick
import QtQuick.Controls
import "qrc:/qml/styles" as Styles

Control {
    id: control

    // 基础属性
    property alias text: label.text
    property bool pressed: mouseArea.pressed
    property bool isHovered: mouseArea.containsMouse
    property bool enabled: true

    // 自定义属性
    property string buttonType: "primary"  // primary, secondary, danger, success, warning
    property string size: "normal"        // small, normal, large
    property string iconSource: ""        // 图标路径或unicode
    property bool iconIsImage: {
        var src = control.iconSource ? String(control.iconSource) : "";
        if (src.length === 0)
            return false;
        var lower = src.toLowerCase();
        if (lower.indexOf("qrc:/") === 0 || lower.indexOf("file:/") === 0)
            return true;
        var suffixes = [".svg", ".png", ".jpg", ".jpeg"];
        for (var i = 0; i < suffixes.length; ++i) {
            var suffix = suffixes[i];
            if (lower.lastIndexOf(suffix) === lower.length - suffix.length) {
                return true;
            }
        }
        return false;
    }
    property bool iconOnLeft: true       // 图标位置
    property bool loading: false         // 加载状态

    // 信号
    signal clicked()
    signal buttonPressed()
    signal buttonReleased()

    // 根据类型获取颜色
    function getBackgroundColor() {
        if (!control.enabled) return Styles.Theme.surfaceVariant;

        switch (buttonType) {
            case "primary": return control.pressed ? Styles.Theme.primaryDark : Styles.Theme.primary;
            case "secondary": return control.pressed ? Styles.Theme.surfaceVariant : Styles.Theme.surface;
            case "danger": return control.pressed ? "#C0392B" : Styles.Theme.danger;
            case "success": return control.pressed ? "#27AE60" : Styles.Theme.success;
            case "warning": return control.pressed ? "#E67E22" : Styles.Theme.warning;
            default: return control.pressed ? Styles.Theme.primaryDark : Styles.Theme.primary;
        }
    }

    function getTextColor() {
        if (!control.enabled) return Styles.Theme.textTertiary;

        switch (buttonType) {
            case "secondary": return Styles.Theme.textPrimary;
            default: return Styles.Theme.textOnPrimary;
        }
    }

    function getHoverColor() {
        switch (buttonType) {
            case "primary": return "#5DADE2";
            case "secondary": return Styles.Theme.surfaceVariant;
            case "danger": return "#EC7063";
            case "success": return "#58D68D";
            case "warning": return "#F7DC6F";
            default: return "#5DADE2";
        }
    }

    // 根据尺寸设置高度
    height: {
        switch (size) {
            case "small": return Styles.Theme.buttonHeightSmall;
            case "large": return Styles.Theme.buttonHeightLarge;
            default: return Styles.Theme.buttonHeightNormal;
        }
    }

    // 最小宽度
    implicitWidth: Math.max(80, contentItem.implicitWidth + leftPadding + rightPadding)

    // 内边距
    leftPadding: Styles.Theme.spacingNormal
    rightPadding: Styles.Theme.spacingNormal
    topPadding: Styles.Theme.spacingSmall
    bottomPadding: Styles.Theme.spacingSmall

    // 背景样式
    background: Rectangle {
        radius: Styles.Theme.radiusNormal
        color: control.getBackgroundColor()
        border.color: buttonType === "secondary" ? Styles.Theme.border : "transparent"
        border.width: buttonType === "secondary" ? 1 : 0

        // 悬停效果
        Rectangle {
            anchors.fill: parent
            radius: parent.radius
            color: control.getHoverColor()
            opacity: control.isHovered && !control.pressed ? 0.3 : 0

            Behavior on opacity {
                NumberAnimation { duration: Styles.Theme.animationFast }
            }
        }

        // 加载动画背景
        Rectangle {
            visible: control.loading
            anchors.fill: parent
            radius: parent.radius
            color: "black"
            opacity: 0.3
        }
    }

    // 内容布局
    contentItem: Item {
        implicitWidth: row.implicitWidth
        implicitHeight: row.implicitHeight

        Row {
            id: row
            anchors.centerIn: parent
            spacing: (control.iconSource !== "" && control.text !== "") ? Styles.Theme.spacingSmall : 0
            layoutDirection: control.iconOnLeft ? Qt.LeftToRight : Qt.RightToLeft

            // 图标（图片）
            Image {
                visible: control.iconSource !== "" && !control.loading && control.iconIsImage
                source: visible ? control.iconSource : ""
                anchors.verticalCenter: parent.verticalCenter
                fillMode: Image.PreserveAspectFit
                width: {
                    switch (control.size) {
                        case "small": return 16;
                        case "large": return 24;
                        default: return 20;
                    }
                }
                height: width
            }

            // 图标（文本/emoji）
            Text {
                visible: control.iconSource !== "" && !control.loading && !control.iconIsImage
                text: control.iconSource
                color: control.getTextColor()
                font.pixelSize: {
                    switch (control.size) {
                        case "small": return 14;
                        case "large": return 20;
                        default: return 16;
                    }
                }
                anchors.verticalCenter: parent.verticalCenter
            }

            // 文本
            Text {
                id: label
                visible: control.text !== "" && !control.loading
                color: control.getTextColor()
                font.pixelSize: {
                    switch (control.size) {
                        case "small": return Styles.Theme.fontSizeSmall;
                        case "large": return Styles.Theme.fontSizeMedium;
                        default: return Styles.Theme.fontSizeNormal;
                    }
                }
                font.weight: Styles.Theme.fontWeightMedium
                anchors.verticalCenter: parent.verticalCenter
            }

            // 加载指示器
            Rectangle {
                visible: control.loading
                width: 20
                height: 20
                radius: 10
                color: "transparent"
                border.width: 2
                border.color: control.getTextColor()
                anchors.verticalCenter: parent.verticalCenter

                Rectangle {
                    width: 6
                    height: 6
                    radius: 3
                    color: control.getTextColor()
                    anchors.centerIn: parent

                    RotationAnimation on rotation {
                        running: control.loading
                        loops: Animation.Infinite
                        from: 0
                        to: 360
                        duration: 1000
                    }
                }
            }
        }
    }

    // 鼠标交互
    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true
        cursorShape: Qt.PointingHandCursor
        enabled: control.enabled && !control.loading

        onClicked: control.clicked()
        onPressed: control.buttonPressed()
        onReleased: control.buttonReleased()
    }

    // 按下动画（可选）
    scale: pressed ? 0.95 : 1.0
    Behavior on scale {
        NumberAnimation { duration: Styles.Theme.animationFast }
    }

    // 不透明度动画
    opacity: enabled ? 1.0 : 0.6
    Behavior on opacity {
        NumberAnimation { duration: Styles.Theme.animationFast }
    }
}
